//
//  MainWindowController.m
//  Fusion_Network_Manager
//
//  Created by Brandon on 4/27/12.
//  Copyright (c) 2012 PWNu8s. All rights reserved.
//

#import "MainWindowController.h"

@interface MainWindowController ()

@end

@implementation MainWindowController

- (id)initWithWindow:(NSWindow *)window
{
    self = [super initWithWindow:window];
    if (self) {
        // Initialization code here.
        network = [[NSMutableDictionary alloc] init];
    }
    return self;
}

-(void)setInitialValues{
    NSString * origString = [NSString stringWithContentsOfFile: @"/Users/bdm7146/networking" encoding:NSASCIIStringEncoding error:NULL];
    origString = [origString stringByReplacingOccurrencesOfString:@"answer VNET_" withString:@""];
    origString = [origString stringByReplacingOccurrencesOfString:@"VERSION=1,0" withString:@""];
    
    NSArray * strArray = [origString componentsSeparatedByString:@"\n"];
    NSMutableDictionary *vmNumbers = [[NSMutableDictionary alloc] init];
    //Initially loop through the file gaining only the vm numbers
    for (NSString *strLine in strArray) {
        if([strLine isEqualToString:@""]){
            continue;
        }
        NSString *vmNumber = [strLine substringToIndex:1];
        
        NSMutableDictionary *virtual = [[NSMutableDictionary alloc] init];
        [vmNumbers setValue:virtual forKey:vmNumber];
        //[vmNumbers setValue:vmNumber forKey:vmNumber];
    }
    
    for (NSString *strLine in strArray) {
        if([strLine isEqualToString:@""]){
            continue;
        }
        NSString *value = [strLine substringFromIndex:2];
        NSArray *words = [value componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
        [[vmNumbers objectForKey:[strLine substringToIndex:1]] setValue:[words objectAtIndex:1] forKey:[words objectAtIndex:0]];
        NSLog(@"Adding (%@) setting %@,%@",[strLine substringToIndex:1],[words objectAtIndex:0],[words objectAtIndex:1]);
    }
    
    // Read values now for DHCP and NAT
    
    for(NSString *key in vmNumbers){
        //NSLog(@"KEYS: %@",key);
        NSString *vmName = @"vmnet";
        vmName = [vmName stringByAppendingString:key];
        NSLog(@"VMName:  %@",vmName);
    }
    
    
    for(NSString *key in vmNumbers){
        NSDictionary *dict = [vmNumbers objectForKey:key];
        VMNet *vm = [[VMNet alloc] initWithDictionary:dict];
        NSString *vmName = @"VM";
        vmName = [vmName stringByAppendingString:key];
        [network setObject:vm forKey:vmName];
        NSLog(@"Dictionary = %@",dict);
    }
    
    //*****  Weird ********
    [dhcpRange1 setStringValue:@"10.1.1.90"];
    [dhcpRange2 setStringValue:@"10.1.1.100"];
    [dhcpSubnet setStringValue:@"255.255.255.0"];
    [dhcpNetwork setStringValue:@"10.1.1.0"];
    [dhcpBroadcast setStringValue:@"10.1.1.255"];
    [dnsDomain setStringValue:@"pwnubs.net"];
    [dnsServer1 setStringValue:@"129.21.3.17"];
    [dnsServer2 setStringValue:@"8.8.8.8"];
    [dnsMaxLeaseTime setStringValue:@"1800"];
    [dnsDefaultLeaseTime setStringValue:@"1800"];
    NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
    
    if([dhcpEnabled state] == NSOnState)
        [dictionary setObject:@"yes" forKey:@"dhcpEnabled"];
    else
        [dictionary setObject:@"no" forKey:@"dhcpEnabled"];
    //Load Values from File
    [dictionary setObject:@"VMNET_1" forKey:@"vmLabel"];
    [dictionary setObject:@"yes" forKey:@"vmEnabled"];
    [dictionary setObject:@"yes" forKey:@"natEnabled"];
    [dictionary setObject:@"1" forKey:@"vmID"];
    [dictionary setObject:@"10.1.1.0" forKey:@"dhcpNetwork"];
    [dictionary setObject:@"10.1.1.255" forKey:@"dhcpBroadcast"];
    [dictionary setObject:@"255.255.255.0" forKey:@"dhcpSubnet"];
    [dictionary setObject:@"10.1.1.5" forKey:@"dhcpRange2"];
    [dictionary setObject:@"10.1.1.1" forKey:@"dhcpRange1"];
    [dictionary setObject:@"example1.com" forKey:@"dnsDomain"];
    [dictionary setObject:@"10.1.1.1" forKey:@"dnsServer1"];
    [dictionary setObject:@"10.1.1.1" forKey:@"dnsServer2"];
    [dictionary setObject:@"1800" forKey:@"dnsDefaultLeaseTime"];
    [dictionary setObject:@"1800" forKey:@"dnsMaxLeaseTime"];
    
    
    if([dhcpEnabled state] == NSOnState)
        [dictionary setObject:@"yes" forKey:@"dhcpEnabled"];
    else
        [dictionary setObject:@"no" forKey:@"dhcpEnabled"];
    
    VMNet *vmnet1 = [[VMNet alloc] initWithDictionary:dictionary];
    [dictionary removeAllObjects];
    [dictionary setObject:@"VMNET_8" forKey:@"vmLabel"];
    [dictionary setObject:@"yes" forKey:@"vmEnabled"];
    [dictionary setObject:@"yes" forKey:@"natEnabled"];
    [dictionary setObject:@"yes" forKey:@"dhcpEnabled"];
    [dictionary setObject:@"8" forKey:@"vmID"];
    [dictionary setObject:@"10.1.1.1" forKey:@"dhcpNetwork"];
    [dictionary setObject:@"10.1.1.255" forKey:@"dhcpBroadcast"];
    [dictionary setObject:@"255.255.255.0" forKey:@"dhcpSubnet"];
    [dictionary setObject:@"10.1.1.5" forKey:@"dhcpRange2"];
    [dictionary setObject:@"10.1.1.1" forKey:@"dhcpRange1"];
    [dictionary setObject:@"example2.com" forKey:@"dnsDomain"];
    [dictionary setObject:@"10.1.1.1" forKey:@"dnsServer1"];
    [dictionary setObject:@"10.1.1.1" forKey:@"dnsServer2"];
    [dictionary setObject:@"1800" forKey:@"dnsDefaultLeaseTime"];
    [dictionary setObject:@"1800" forKey:@"dnsMaxLeaseTime"];
    VMNet *vmnet8 = [[VMNet alloc] initWithDictionary:dictionary];
    
    [network setObject:vmnet1 forKey:@"VMNet1"];
    [network setObject:vmnet8 forKey:@"VMNet8"];
    
    
    
    //Done
    [pullDown removeAllItems];
    for (id key in network) {
        NSLog(@"KEY = %@",key);
        [pullDown addItemWithTitle:key];    
    }
    VMNet  *vmnet = [network objectForKey:[pullDown titleOfSelectedItem]];
    
    // MUST EDIT
    [self setValueFromVMNet:vmnet];
}


//Done
-(void)setValueFromVMNet:(VMNet *)net{
    NSLog(@"setValueFromVMNet");
    [dhcpNetwork setStringValue:net->dhcpNetwork];
    [dhcpBroadcast setStringValue:net->dhcpBroadcast];
    [dhcpRange1 setStringValue:net->dhcpRange1];
    [dhcpRange2 setStringValue:net->dhcpRange2];
    [dhcpSubnet setStringValue:net->dhcpSubnet];
    [dhcpEnabled setState:net->dhcpEnableState];
    [dhcpAllowUnknownHosts setState:net->dhcpAllowUnknownHostsState];
    [dnsDomain setStringValue:net->dnsDomain];
    [dnsServer1 setStringValue:net->dnsServer1];
    [dnsServer2 setStringValue:net->dnsServer2];
    [dnsDefaultLeaseTime setStringValue:net->dnsDefaultLeaseTime];
    [dnsMaxLeaseTime setStringValue:net->dnsMaxLeaseTime];
    //[interfaceEnabled setSelectedSegment:net->interfaceState];
}
- (IBAction)dropDownSelected:(id)sender{
    VMNet  *vmnet = [network objectForKey:[pullDown titleOfSelectedItem]];
    [self setValueFromVMNet:vmnet];
}
-(void)awakeFromNib{
    //NSLog(@"awaking");
    [self setInitialValues];
}
- (IBAction)close:(id)sender{
    NSLog(@"Quitting");
    exit(0);
}
- (void)windowDidLoad
{
    [super windowDidLoad];
    NSLog(@"windowDidLoad");
    // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
}
@end
